﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class BindCheckBoxColumn : ITemplate
    {
        private string bindField;
        private string trueValue;

        public BindCheckBoxColumn(string field, string trueValue)
        {
            this.bindField = field;
            this.trueValue = trueValue;
        }

        public void InstantiateIn(Control container)
        {
            CheckBox child = new CheckBox();
            child.DataBinding += new EventHandler(this.OnDataBinding);
            container.Controls.Add(child);
        }

        public void OnDataBinding(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox) sender;
            GridViewRow namingContainer = (GridViewRow) box.NamingContainer;
            if (((DataRowView) namingContainer.DataItem).Row[this.bindField] == null)
            {
                box.Checked = false;
            }
            else if (((DataRowView) namingContainer.DataItem).Row[this.bindField].ToString().Trim() == this.trueValue)
            {
                box.Checked = true;
            }
            else
            {
                box.Checked = false;
            }
        }
    }
}

